package com.ycy.temperature.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.jude.utils.JUtils;
import com.ycy.temperature.R;
import com.ycy.temperature.bean.ActivityBean;
import com.ycy.temperature.bean.DynamicBean;
import com.ycy.temperature.bean.VerificationBean;
import com.ycy.temperature.date.net.UserNetWorks;
import com.ycy.temperature.view.view.RoundImageView;
import com.zhy.autolayout.AutoLinearLayout;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.List;

import rx.Subscriber;

/**
 * @author Jirh
 */
public class ActivityAdapter extends BaseQuickAdapter<ActivityBean.DataBean, BaseViewHolder> {

    public ActivityAdapter(int layoutResId, List data) {
        super(layoutResId, data);
    }

    @Override
    protected void convert(final BaseViewHolder holder, final ActivityBean.DataBean cate) {

        holder.setIsRecyclable(false);
        RoundImageView ImageHeadImage = holder.getView(R.id.Image_HeadImage);
        TextView TextNickName = holder.getView(R.id.Text_NickName);
        TextView TextReleaseaddress = holder.getView(R.id.Text_Releaseaddress);
        TextView TextContent = holder.getView(R.id.Text_Content);
        TextView TextTitle = holder.getView(R.id.Text_Title);
        TextView TextTime = holder.getView(R.id.Text_Time);
        Button ButtonGO = holder.getView(R.id.Button_GO);
        AutoLinearLayout LayoutLabel = holder.getView(R.id.Layout_Label);
        Glide.with(mContext)
                .load("http://" + cate.getActivityUserMessage().getHeadimage())
                .apply(new RequestOptions()
                        .centerCrop()
                        .placeholder(R.mipmap.ic_launcher)
                        .error(R.mipmap.ic_launcher)
                        .diskCacheStrategy(DiskCacheStrategy.ALL))
                .into(ImageHeadImage);
        TextNickName.setText(cate.getActivityUserMessage().getNickname());
        TextReleaseaddress.setText(cate.getReleaseaddress());
        TextContent.setText(cate.getContent());
        TextTitle.setText(cate.getTitle());
        TextTime.setText("时间:" + cate.getActivitytime());
        String[] arr = cate.getLableid().replace("[", "").replace("]", "").split(",");
        List<String> CirleList = Arrays.asList(arr);
        for (int i = 0; i < CirleList.size(); i++) {
            addOrderItem(CirleList.get(i), LayoutLabel);
        }
        ButtonGO.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
    }

    public void addOrderItem(final String LabelList, LinearLayout ll_lv) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.adapter_activity_label, null);
        TextView TextLabel = view.findViewById(R.id.Text_Label);
        TextLabel.setText(LabelList);
        ll_lv.addView(view);
    }

    private String getDate(String date) {
        long day = 0;
        long hour = 0;
        long min = 0;
        long sec = 0;
        long diff;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        try {
            diff = System.currentTimeMillis() - sdf.parse(date).getTime();
            day = diff / (24 * 60 * 60);
            hour = (diff / (60 * 60) - day * 24);
            min = ((diff / 60) - day * 24 * 60 - hour * 60);
            sec = (diff / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        if (day != 0) {
            if (day >= 2) {
                return date;
            } else {
                return day + "天前";
            }
        }
        if (hour != 0) return hour + "小时前";
        if (min != 0) return min + "分钟前";
        if (sec != 0) return "1分钟前";
        return "0秒";

    }

    public void getFabulous(String Createuserid, String Circleid, final ImageView mImage, final TextView mText, final DynamicBean.DataBean mDateBean) {
        UserNetWorks.getFabulous(Createuserid, Circleid, new Subscriber<VerificationBean>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                e.printStackTrace();
                JUtils.Toast("网络错误");
            }

            @Override
            public void onNext(VerificationBean verificationBean) {
                if (verificationBean.getResponseState().equals("1")) {
                    if (mDateBean.getPraiseStatus().equals("0")) {
                        mDateBean.setPraiseStatus("1");
                        mImage.setImageResource(R.mipmap.image_fabulous_true);
                        mText.setText((Integer.valueOf(mText.getText().toString()) + 1) + "");
                    } else if (mDateBean.getPraiseStatus().equals("1")) {
                        mDateBean.setPraiseStatus("0");
                        mImage.setImageResource(R.mipmap.image_fabulous);
                        mText.setText((Integer.valueOf(mText.getText().toString()) - 1) + "");
                    }
                }
            }
        });
    }
}
